﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using TDBaseDataLibrary;
namespace TDEngineLibrary
{

    public class BaseEquipment
    {
        public int id;
        public string type;    //装备种类
        public string caption;
        public string icon;
        public string descript;
        public int[] attribute;
        public int pos;         //放置位置。
        public int Durability;//耐久度
    };
    public class TDEquipment
    {
        public int id;
        public int Durability;
        public int[] Mosaic;
    }
    public class RoleClass
    {
        public RoleClass()
        {
            attribute = new int[32];
        }
        static RoleClasss g_RoleClasss;
        public int id;
        public string caption;
        string descript;
        string tex;
        RoleSprite sprite;
        public RoleSprite Sprite{
            get { return sprite; }
            set { sprite = value; }
        }
        public static string[] attrID =
        {
            "lift",
            "ac",
            "dc",
            "str",
            "power",
            "luck",
            "speed",
            "downforce",
            "price",
        };
        public static string[] attrcaption =
        {
            "生命",
            "攻击力",
            "防御力",
            "力量",
            "魔法",
            "运气",
            "速度",
            "可吸收法力",
            "价格",
        };
        public int[] attribute;
        public void set_attribute(enumattr attr,int value)
        {
            attribute[(int)attr] = value;
        }
        public void set_attribute(int[] values)
        {
            int ncount = attribute.GetLength(0);
            for (int i = 0; i < ncount; i++)
            {
                attribute[i] = values[i];
            }
        }
        static public void LoadContent(ContentManager content)
        {
            g_RoleClasss = content.Load<RoleClasss>("Enemy_Config");
        }
        static public RoleClass[] LoadRoleClass(ContentManager content,string szfile)
        {
            string line;
            RoleClass[] rolearray;
            using (StreamReader reader = new StreamReader(szfile))
            {
                int nCount = 0;
                line = reader.ReadLine();
                line.Trim();
                string[] arrow = line.Split('=');
                if (arrow[0] == "count")
                {
                    nCount = int.Parse(arrow[1]);
                }
                rolearray = new RoleClass[nCount];
                for (int i = 0; i < nCount; i++)
                {
                    rolearray[i] = new RoleClass();
                    if (rolearray[i].load_role(reader,content) == 0)
                        break;
                }
            }
            return rolearray;
        }
        char[] stopchar = {(char)0x0020};
        //public int load_role(InfoReader reader, ContentManager content)
        //{
        //    string strname = "";
        //    string strvalue = "";
        //    while (!reader.iseof())
        //    {
        //        bool bret = reader.nextline(ref strname, ref strvalue);
        //        if (!bret)
        //            continue;
        //        if (strname == "caption")
        //        {
        //            caption = strvalue;
        //        }
        //        else if (strname == "descript")
        //        {
        //            descript = strvalue;
        //        }
        //        else if (strname == "tex")
        //        {
        //            tex = strvalue;
        //            Vector2 vExCenter = new Vector2(40, 40);
        //            sprite = new RoleSprite(content, tex, 200, true, 64, 64, 13, vExCenter);
        //        }
        //        else if (strname == "end")
        //        {
        //            return 1;
        //        }
        //        else
        //        {
        //            load_attribute(strname, strvalue);
        //        }
        //    }
        //    return 1;
        //}
        public int load_info(string szname, string szinfo)
        {
            return 1;
        }
        public int load_role(StreamReader reader, ContentManager content)
        {
            string line;
            do
            {
                line = reader.ReadLine();
                if (line == null)
                    break;
                line.TrimStart();
                line.TrimEnd();
                if (line.Length == 0)
                    continue;
                
                string[] arrow = line.Split('=');
                arrow[0].TrimStart();
                arrow[0].TrimEnd(stopchar);
                arrow[0].Trim(stopchar);
                if (arrow[0] == "caption")
                {
                    caption = arrow[1];
                }
                else if (arrow[0] == "descript")
                {
                    descript = arrow[1];
                }
                else if (arrow[0] == "tex")
                {
                    tex = arrow[1];
                    Vector2 vExCenter = new Vector2(40,40);
                    sprite = new RoleSprite(content, tex, 200, true, 64, 64, 13, vExCenter);
                }
                else if (arrow[0] == "end")
                {
                    return 1;
                }
                else
                {
                    load_attribute(arrow[0], arrow[1]);
                }
            } while (line != null);
            return 0;
        }


        public void load_attribute(string szname,string szvalue)
        {
            int nSize = attrcaption.GetLength(0);
            for (int i = 0; i < nSize; i++)
            {
                if (szname == attrID[i])
                {
                    attribute[i] = int.Parse(szvalue);
                }
            }
        }
        public void load_attribute(string szfile)
        {
            string line;
            using (StreamReader reader = new StreamReader(szfile))
            {
                do
                {
                    line = reader.ReadLine();
                    if (line == null)
                        break;
                    string[] arrow = line.Split('=');
                    int nSize = attrcaption.GetLength(0);
                    for(int i = 0;i < nSize;i ++)
                    {
                        if (arrow[0] == attrID[i])
                        {
                            attribute[i] = int.Parse(arrow[1]);
                        }
                    }
                } while (line != null);
            }
        }
        //得到属性值
        public int get_attribute(enumattr attr)
        {
            return attribute[(int)attr];
        }
    };
}
